//app.js
App({
  onLaunch: function () {
  
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        this.globalData.code = res.code;
      }
    });
    // 获取用户信息
    wx.getUserInfo({
      success: res => {
        // 可以将 res 发送给后台解码出 unionId
        this.globalData.userInfo = res.userInfo;
        var code = this.globalData.code;
        var encryptedData = res.encryptedData;
        var iv = res.iv;
        var basePath = this.globalData.basePath.miniAppPath;
         var userData = {
           code:code,
           encryptedData:encryptedData,
           iv:iv
         };
         console.log(userData);
       
        wx.request({
          url: basePath+'/wxMa/0',
          data: userData,
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success:function(res){
            
            getApp().globalData.sessionId = res.data.sessionId;
                  if(res.data.code=="22"){
                      getApp().globalData.userId = res.data.userId;
                      console.log(res.data.userId);
                  }
                if(res.data.code=="21"){
                    // ask
                wx.showModal({
                  title: '初次登陆绑定提醒',
                  content: '是否绑定已有学艺宝账户',
                  success:function(res){
                    if(res.confirm){
                       wx.navigateTo({
                         url: '/pages/center/bind/bind',
                       });
                    }else if(res.cancel){
                      var sessionId = this.globalData.sessionId;
                      wx.request({
                        url: basePath+'/wxMa/2',
                        header: {
                          'content-type': 'application/x-www-form-urlencoded',
                          'Cookie':"JSESSIONID="+sessionId
                        },
                        success:function(res){
                              if(res.data.code=="10"){
                                  wx.showToast({
                                    title: '授权失败，请退出重试',
                                  });
                              }else{
                                getAPP().globalData.userId = res.data.userId;
                              }
                        }
                      });
                    }
                  }
                });

                }else if(res.data.code=="20"){
                     wx.showToast({
                       title: '网络异常，稍后重试',
                     });
                }
          }
        });

      

        // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
        // 所以此处加入 callback 以防止这种情况
        if (this.userInfoReadyCallback) {
          this.userInfoReadyCallback(res);
        }
      }
    });
  },
  globalData: {
    BusinessIDs:{},
    userId:"",
    userInfo: {},
    code: "",
    sessionId: "",
    basePath: {
      mediaPath: "https://media.xueyib.com",
      appPath: "https://tianlai.axibug.com",
      miniAppPath: "https://tunnel1.xueyib.com"
      // appPath: "https://app.xueyib.com",
      // miniAppPath: "https://miniapi.xueyib.com",
    //  testAppPath: "https://tianlai.axibug.com",
    //  testMiniAppPath: "https://tunnel1.xueyib.com"
    }
  }
})